package com.mjfeed.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mjfeed.entity.ForumReply;
import com.mjfeed.entity.ForumTopic;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.ForumReplyService;
import com.mjfeed.dao.ForumReplyDao;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;
import com.mjfeed.util.QueryCondition;

@Service("forumReplyService")
public class ForumReplyServiceImpl extends BaseServiceImpl implements ForumReplyService {
	@Resource(name="forumReplyDao")
    private ForumReplyDao forumReplyDao;
    
    @Transactional(readOnly = true)
	public Pagination<ForumReply> getPagination(ParamBean paramBean) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		if (StringUtils.isNotBlank(paramBean.getKeyword())) {
			queryConditions.add(new QueryCondition("content", QueryCondition.LK, paramBean.getKeyword()));
		}
		if (paramBean.getProductCategoryId() != null && paramBean.getProductCategoryId().longValue() > 0) {
			ProductCategory productCategory = getById(ProductCategory.class, paramBean.getProductCategoryId());
			if (productCategory != null) {
				queryConditions.add(new QueryCondition("productCategory", QueryCondition.EQ, productCategory));
			}
		}
		if (paramBean.getForumTopicId() != null && paramBean.getForumTopicId().longValue() > 0) {
			queryConditions.add(new QueryCondition("forumTopicId", QueryCondition.EQ, paramBean.getForumTopicId()));
		}
		String orderBy = "order by createDate asc";
		Pagination<ForumReply> pagination = (Pagination<ForumReply>)getPagination(ForumReply.class, queryConditions, orderBy, paramBean.getPage(), paramBean.getPageSize());
		pagination.setPageHtml(paramBean.getUrl());
		return pagination;
	}

    @Transactional
	public void deleteAll(ForumTopic forumTopic) {
    	forumReplyDao.deleteAll(forumTopic);
	}
}
